package com.scau.yangsuiyu.estore.controller.myComponent;

import com.scau.yangsuiyu.estore.dababases.entity.Goods;
import com.scau.yangsuiyu.estore.dababases.entity.GoodsPhoto;
import com.scau.yangsuiyu.estore.dababases.entity.User;
import com.scau.yangsuiyu.estore.service.AppService;
import com.scau.yangsuiyu.estore.util.ImageViewUtil;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.control.Alert;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;

/**
 * @author suiyu_yang
 * @description 主界面的商品界面
 * @date 2019-06-13 01:13
 * @email suiyu_yang@163.com
 */
public class IndexGoodsVBox extends VBox implements Initializable {
    private Goods goods;
    private User user;

    @FXML
    private ImageView photoImageView;

    @FXML
    private HBox subImagesHBox;


    @FXML
    private Label promotionalPriceLabel;

    @FXML
    private Label goodsNameLabel;

    @FXML
    private Label shopNameLabel;

    public IndexGoodsVBox(User user, Goods goods) {
        this.user = user;
        this.goods = goods;
        loadFXML();
    }

    private void loadFXML() {
        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/view/myComponent/IndexGoodsVBox.fxml"));
        fxmlLoader.setRoot(this);
        fxmlLoader.setController(this);
        try {
            fxmlLoader.load();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    @Override
    public void initialize(URL location, ResourceBundle resources) {
        this.setId("vBox");
        setGoodsToUI();
    }

    @FXML
    private void gotoGoodsPane() {
        new AppService().gotoBuyGoodsPane(user, goods);
    }

    @FXML
    private void gotoShopPane() {

    }

    private void setGoodsToUI() {

        int i = 0;
        for (GoodsPhoto goodsPhoto : goods.getGoodsPhotoList()) {
            if (i == 0) {
                photoImageView.setFitHeight(150);
                photoImageView.setFitWidth(150);
                ImageViewUtil.loadImage(photoImageView, goodsPhoto.getPhotoUrl());
            } else {
                ImageView imageView = new ImageView();
                imageView.setFitWidth(50);
                imageView.setFitHeight(50);
                ImageViewUtil.loadImage(imageView, goodsPhoto.getPhotoUrl());
                subImagesHBox.getChildren().add(imageView);
            }
            i++;
        }
        promotionalPriceLabel.setText("¥" + goods.getPromotionPrice());
        goodsNameLabel.setText(goods.getGoodsName());
        shopNameLabel.setText(goods.getShop().getShopName());

    }

}
